home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
src
/
fig07_04.jar
/
Ch07
/
Fig07_04
/
Date1.cpp
next >
Wrap
C/C++ Source or Header
|
1997-10-20
|
2KB
|
60 lines
// Fig. 7.4: date.cpp
// Member function definitions for Date class.
#include <iostream.h>
#include "date1.h"
// Constructor: Confirm proper value for month;
// call utility function checkDay to confirm proper
// value for day.
Date::Date( int mn, int dy, int yr )
{
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
year = yr; // should validate yr
day = checkDay( dy ); // validate the day
cout << "Date object constructor for date ";
print(); // interesting: a print with no arguments
cout << endl;
}
// Print Date object in form month/day/year
void Date::print() const
{ cout << month << '/' << day << '/' << year; }
// Destructor: provided to confirm destruction order
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
}
// Utility function to confirm proper day value
// based on month and year.
// Is the year 2000 a leap year?
int Date::checkDay( int testDay )
{
static const int daysPerMonth[ 13 ] =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && // February: Check for leap year
testDay == 29 &&
( year % 400 == 0 || // year 2000?
( year % 4 == 0 && year % 100 != 0 ) ) ) // year 2000?
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";
return 1; // leave object in consistent state if bad value
}